package com.woan.api.idao.dao;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.woan.api.idao.SysVideoBasicIDAO;
import com.woan.api.model.SysAdmininfo;
import com.woan.api.model.SysVideo;
import com.woan.api.model.SysVideoBasic;
import com.woan.api.util.FuncDate;
import com.woan.api.vo.VClientvideoList;
import com.woan.api.vo.VpositionList;
import com.woan.api.vo.VvideoList;

/**
 * A data access object (DAO) providing persistence and search support for
 * SysVideo entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.woan.api.model.SysVideo
 * @author MyEclipse Persistence Tools
 */

public class SysVideoBasicDAO extends HibernateDaoSupport  implements SysVideoBasicIDAO {
	//根据视频id获取视频对象
	public SysVideoBasic get(String videoid) {
		return (SysVideoBasic) this.getHibernateTemplate().get(SysVideoBasic.class,
				videoid);
	}
	//	根据数据库生成的SysVideo实体类返回列表
	public List<SysVideoBasic> FindLiveVideoByGroup(String groupid, Integer beginIndex,Integer endIndex)
	{
		StringBuffer hql = new StringBuffer("from SysVideo");
		hql.append(" where rid='"+groupid+"' and statusM3u8 = 0 and isdel = '1' order by beginTime desc ");
		Session session = this.getSession();
		try
		{
			Query query = session.createQuery(hql.toString());
			query.setFirstResult(beginIndex);
			query.setMaxResults(endIndex);
			List<SysVideoBasic> list=query.list();
			
			return list;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	//根据自定义返回的实体返回直播列表
	public List<VvideoList> FindVLiveVideoByGroup(String groupid, Integer beginIndex,Integer endIndex)
	{
		StringBuffer buffer = new StringBuffer("select vid, picture, userName, address, detail,videoSize,videoLength,videoPath,beginTime,endTime,title,");
		buffer.append("longitude, latitude, countrycode, countryname,province,city,counties from (");
		buffer.append("select vid, pictureM3u8 as picture, userName, address, detail,videoSize,videoLength,videoPathM3u8 as videoPath,beginTime,endTime,title,");
		buffer.append("longitude, latitude, countrycode, countryname,province,city,counties from  sys_video");
		buffer.append(" where rid='"+groupid+"' and statusM3u8 = 0 and isdel = '1' order by beginTime desc) ab ");
		//System.out.println(buffer.toString());
		try
		{
			Query query = getSession().createSQLQuery(buffer.toString()).setResultTransformer(
					Transformers.aliasToBean(VvideoList.class));
			if(beginIndex!=0)
			{
				query.setFirstResult(beginIndex);
				query.setMaxResults(endIndex);
			}
			List<VvideoList> list=query.list();
			
			return list;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	//根据自定义返回的实体返回历史列表
	public List<VvideoList> FindVHistoryVideoByGroup(String groupid, Integer beginIndex,Integer endIndex)
	{
		StringBuffer buffer = new StringBuffer(" select vid,picture,userName,address,detail,videoSize,videoLength,videoPath,beginTime,endTime,title, ");
		buffer.append(" longitude, latitude, countrycode, countryname,province,city,counties ");
		buffer.append(" from ( ");
		buffer.append(" select vid, pictureM3u8 as picture, userName, address, detail,videoSize,videoLength,videoPathM3u8 as videoPath,beginTime,endTime,title, ");
		buffer.append(" longitude, latitude, countrycode, countryname,province,city,counties from sys_video ");
		buffer.append(" where rid='"+groupid+"' and statusM3u8 = 1 and isdel = '1' order by beginTime desc) ab");
		System.out.println(buffer.toString());
		try
		{
			Query query = getSession().createSQLQuery(buffer.toString()).setResultTransformer(
					Transformers.aliasToBean(VvideoList.class));
			query.setFirstResult(beginIndex);
			query.setMaxResults(endIndex);
			List<VvideoList> list=query.list();
			
			return list;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}

	//根据自定义返回的实体返回搜索列表
	public List<VvideoList> FindVSerachVideoByDetail(String groupid,Integer beginIndex,Integer endIndex,Map<String, String> map)
	{
		StringBuffer buffer = new StringBuffer(
				"select vid, pictureM3u8 as picture, userName, address, detail,videoSize,videoLength,videoPathM3u8 as videoPath,beginTime,endTime,title,"
						+ " longitude, latitude, countrycode, countryname,province,city,counties"
						+ " from sys_video"
						+ " where rid='"+groupid+"'  and statusM3u8 = 1 and isdel = '1' ");
		if(map.get("paishename").toString()!=null && !map.get("paishename").equals(""))
			buffer.append(" and userName like '%"+map.get("paishename").toString()+"%' ");
		if(map.get("title").toString()!=null && !map.get("title").equals(""))
			buffer.append(" and title like '%"+map.get("title").toString()+"%' ");
		if(map.get("detail").toString()!=null && !map.get("detail").equals("")){
			buffer.append(" and detail like '%"+map.get("detail").toString()+"%' ");
		}
		
		buffer.append(" order by beginTime desc ");
		//System.out.println(buffer.toString());
		try
		{
			Query query = getSession().createSQLQuery(buffer.toString()).setResultTransformer(
					Transformers.aliasToBean(VvideoList.class));
			query.setFirstResult(beginIndex);
			query.setMaxResults(endIndex);
			List<VvideoList> list=query.list();
			
			
			return list;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	//编辑视频描述
	public String EditVideos(String videoid,String videodetail)
	{
		SysVideoBasic sysvideo = this.get(videoid);
		
		String flag = "0";
		if(sysvideo!=null)
		{
			try
			{
				sysvideo.setDetail(videodetail);
				this.getHibernateTemplate().saveOrUpdate(sysvideo);
			}
			catch(Exception e){
				e.printStackTrace();
				flag="1";
			}
		}
		return flag;
	}
	public String EditVideos(String rid,String uid,String username,String vid,String title,String address,String longitude,String latitude,String detail,String countrycode,String countryname,String province,String city,String counties)
	{
		SysVideoBasic sysvideo = this.get(vid);
		String flag = "0";
		if(sysvideo!=null)
		{
			//如果sysvideo不为空，则为修改标题描述的功能
			//如果sysvideo为空，则为拍摄视频的时候调用的更新地理位置信息
			if(title!=null && !title.equals(""))
				sysvideo.setTitle(title);
			if(detail!=null && !detail.equals(""))
				sysvideo.setDetail(detail);
//			if(latitude!=null && !latitude.equals(""))
//				sysvideo.setLatitude(latitude);
//			if(longitude!=null && !longitude.equals(""))
//				sysvideo.setLongitude(longitude);
//			if(countrycode!=null && !countrycode.equals(""))
//				sysvideo.setCountrycode(countrycode);
//			if(countryname!=null && !countryname.equals(""))
//				sysvideo.setCountryname(countryname);
//			if(province!=null && !province.equals(""))
//				sysvideo.setProvince(province);
//			if(counties!=null && !counties.equals(""))
//				sysvideo.setCounties(counties);
//			if(city!=null && !city.equals(""))
//				sysvideo.setCity(city);
//			sysvideo.setAddress(province+city+counties);
		}
		else
		{
			sysvideo = new SysVideoBasic();
			sysvideo.setVid(vid);
			sysvideo.setRid(rid);
			sysvideo.setUid(uid);
			sysvideo.setUserName(username);
			sysvideo.setCreateTime(FuncDate.FindCurSysTime1());
			if(title!=null && !title.equals(""))
				sysvideo.setTitle(title);
			else
				sysvideo.setTitle("视频直播");
			if(detail!=null && !detail.equals(""))
				sysvideo.setDetail(detail);
			else
				sysvideo.setDetail("暂无描述");
			if(latitude!=null && !latitude.equals(""))
				sysvideo.setLatitude(latitude);
			if(longitude!=null && !longitude.equals(""))
				sysvideo.setLongitude(longitude);
			if(countrycode!=null && !countrycode.equals(""))
				sysvideo.setCountrycode(countrycode);
			if(countryname!=null && !countryname.equals(""))
				sysvideo.setCountryname(countryname);
			if(province!=null && !province.equals(""))
				sysvideo.setProvince(province);
			if(counties!=null && !counties.equals(""))
				sysvideo.setCounties(counties);
			if(city!=null && !city.equals(""))
				sysvideo.setCity(city);
			
			sysvideo.setAddress(province+city+counties);
		}
		try
		{
			this.getHibernateTemplate().saveOrUpdate(sysvideo);
		}
		catch(Exception e){
			e.printStackTrace();
			System.out.println(e);
			flag="1";
		}
		return flag;
	}

	//位置信息
	public List<VpositionList> FindVVideoPostion(String groupid)
	{
		StringBuffer buffer = new StringBuffer(
				"SELECT ad.nickName,ad.userName,ad.longitude,ad.latitude,ad.province,ad.city,ad.counties," +
				"ad.countryname,v.address,v.videoLength,v.videoPath,v.videoSize,v.picture,v.title," +
				"v.beginTime,v.endTime FROM sys_admininfo ad LEFT JOIN sys_video v ON ad.uid = v.uid " +
				"where (v.status is null or v.status='0') and ad.rid='"+groupid+"'");
		//System.out.println(buffer.toString());
		try
		{
			Query query = getSession().createSQLQuery(buffer.toString()).setResultTransformer(
					Transformers.aliasToBean(VpositionList.class));
			
			List<VpositionList> list=query.list();
			
			return list;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	//根据自定义返回的实体返回直播列表
	public List<VClientvideoList> FindVCLiveVideoByGroup(String groupid, Integer beginIndex,Integer endIndex)
	{
		StringBuffer buffer = new StringBuffer("select v.vid, v.pictureFlv, v.pictureM3u8, v.userName, v.address, v.detail,v.videoPathFlv,v.videoPathM3u8,v.beginTime,v.endTime,v.title,a.nickName ");
		buffer.append(" from sys_video v ,sys_admininfo a");
		buffer.append(" where v.rid='"+groupid+"' and a.uid=v.uid and v.statusM3u8 = 0 and v.statusFlv = 0 and v.isdel = '1' order by v.beginTime desc ");
		System.out.println(buffer.toString());
		try
		{
			Query query = getSession().createSQLQuery(buffer.toString()).setResultTransformer(
					Transformers.aliasToBean(VClientvideoList.class));
			if(beginIndex!=0)
			{
				query.setFirstResult(beginIndex);
				query.setMaxResults(endIndex);
			}
			List<VClientvideoList> list=query.list();
			
			return list;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	//根据自定义返回的实体返回历史列表
	public List<VClientvideoList> FindVCHistoryVideoByGroup(String groupid, Integer beginIndex,Integer endIndex)
	{
		StringBuffer buffer = new StringBuffer("select v.vid, v.pictureFlv, v.pictureM3u8, v.userName, v.address, v.detail,v.videoPathFlv,v.videoPathM3u8,v.beginTime,v.endTime,v.title,v.videoLength,a.nickName ");
		buffer.append(" from sys_video v ,sys_admininfo a");
		buffer.append(" where v.rid='"+groupid+"' and a.uid=v.uid and v.statusM3u8 = 1 and v.statusFlv = 1 and v.isdel = '1' order by v.beginTime desc ");
		System.out.println(buffer.toString());
		try
		{
			Query query = getSession().createSQLQuery(buffer.toString()).setResultTransformer(
					Transformers.aliasToBean(VClientvideoList.class));
			query.setFirstResult(beginIndex);
			query.setMaxResults(endIndex);
			List<VClientvideoList> list=query.list();
			
			return list;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	//根据自定义返回的实体返回我的视频
	public List<VClientvideoList> FindVMyVideoByUid(String uid, Integer beginIndex,Integer endIndex)
	{
		StringBuffer buffer = new StringBuffer("select v.vid, v.pictureFlv, v.pictureM3u8, v.userName, v.address, v.detail,v.videoPathFlv,v.videoPathM3u8,v.beginTime,v.endTime,v.title,v.videoLength,a.nickName ");
		buffer.append(" from sys_video v ,sys_admininfo a");
		buffer.append(" where v.uid='"+uid+"'  and a.uid=v.uid and v.statusM3u8 = 1 and v.statusFlv = 1 and v.isdel = '1' order by beginTime desc ");
		System.out.println(buffer.toString());
		try
		{
			Query query = getSession().createSQLQuery(buffer.toString()).setResultTransformer(
					Transformers.aliasToBean(VClientvideoList.class));
			query.setFirstResult(beginIndex);
			query.setMaxResults(endIndex);
			List<VClientvideoList> list=query.list();
			
			return list;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	//历史列表数量
	public int FindHistoryVideoCountByGroup(String groupid)
	{
		String hql="from SysVideo where rid='"+groupid+"' and statusM3u8 = 1  and statusFlv = 1 and isdel = '1'";
		try
		{
			List list=this.getHibernateTemplate().find(hql);
			if(list.size()>0)
				return list.size();
			else
				return 0;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return 0;
	}
	//我的视频列表数量
	public int FindMyVideoCountByUid(String uid)
	{
		String hql="from SysVideo where uid='"+uid+"' and statusM3u8 = 1  and statusFlv = 1 and isdel = '1'";
		try
		{
			List list=this.getHibernateTemplate().find(hql);
			if(list.size()>0)
				return list.size();
			else
				return 0;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return 0;
	}
	//更新视频状态-----拍摄之前首先将之前的未结束的视频结束
	public void UpdateVideoState(String uid)
	{
		String hql="update sys_video,sys_video_basic set sys_video.status = 1 where sys_video.vid=sys_video_basic.vid and sys_video_basic.uid='"+uid+"'";
		Session session=this.getSession();
		session.createSQLQuery(hql.toString()).executeUpdate();
	}
	//插入新视频
	public void InsertVideo(SysVideo sysVideo)
	{
		try {
			getHibernateTemplate().save(sysVideo);
		} catch (RuntimeException re) {
			throw re;
		}
	}
	//视频视频id和视频类型查找视频信息
	public SysVideo FindVideoByVidAndType(String vid,String videotype)
	{
		StringBuffer buffer = new StringBuffer("select * from sys_video ");
		buffer.append(" where vid='"+vid+"'  and videotype='"+videotype+"' ");
		System.out.println(buffer.toString());
		try
		{
			Query query = getSession().createSQLQuery(buffer.toString()).setResultTransformer(
					Transformers.aliasToBean(SysVideo.class));
			List<SysVideo> list=query.list();
			return list.get(0);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	//更新视频
	public void UpdateVideo(SysVideo sysVideo)
	{
		try
		{
			this.getHibernateTemplate().save(sysVideo);
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	@Override
	public void UpdateVideoBasic(SysVideoBasic videoBasic) {
		try
		{
			this.getHibernateTemplate().update(videoBasic);
			System.out.println("视频更闹心了，，存到数据库类");
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
	}
	@Override
	public void deletVideoBasic(SysVideoBasic videoBasic_task) {
		try
		{
			this.getHibernateTemplate().delete(videoBasic_task);
			System.out.println("删除成功");
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
	}
	@Override
	public void saveVideoBasic(SysVideoBasic videoBasic) {
		this.getHibernateTemplate().save(videoBasic);
	}
}